//
//  NSArray+SWAdd.m
//  QZMJ
//
//  Created by 腾程－ios1 on 16/12/15.
//  Copyright © 2016年 曾少伟. All rights reserved.
//

#import "NSArray+SWAdd.h"

@implementation NSArray (SWAdd)

- (NSMutableArray *)getSameElementWithExample:(id)example {
    NSMutableArray *marr = @[].mutableCopy;
    for (id obj in self) {
        if ([obj isEqual:example]){
            [marr addObject:obj];
        }
    }
    return marr;
}

- (NSInteger)getSameElementCountWithExample:(id)example {
    return [self getSameElementWithExample:example].count;
}

- (NSMutableArray *)noSameElement {
    NSMutableArray *marr = [NSMutableArray new];
    for (id obj in self) {
        if (![marr containsObject:obj]) {
            [marr addObject:obj];
        }
    }
    return marr;
}

- (NSMutableArray *)sw_exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2 {
    NSMutableArray *arrCopy = self.mutableCopy;
    [arrCopy exchangeObjectAtIndex:idx1 withObjectAtIndex:idx2];
   return arrCopy;
}

- (NSMutableArray *)sw_addObject:(id)anObject {
    NSMutableArray *arrCopy = self.mutableCopy;
    [arrCopy addObject:anObject];
    return arrCopy;
}

- (NSMutableArray *)sw_moveFirstObjectToLast {
    NSMutableArray *arrCopy = self.mutableCopy;
    id obj = arrCopy.firstObject;
    [arrCopy removeFirstObject];
    [arrCopy addObject:obj];
    return arrCopy;
}

@end


@implementation NSMutableArray (SWAdd)


- (void)removeOneObject:(nullable id)obj {
    NSAssert([self containsObject:obj], @"no exit obj");
    
    NSInteger idx = [self indexOfObject:obj];
    [self removeObjectAtIndex:idx];
}

- (NSMutableArray *)randomArr {
    NSInteger count = self.count - 1;
    int index;
    id tmp;
    
    for ( ; count > 0; count --) {
        index = rand() % count;
        tmp = self[count];
        self[count] = self[index];
        self[index] = tmp;
    }
    return self;
}

- (void)replaceObject:(id)obj withObj:(id)obj1 {
    if (![self containsObject:obj]) return;
    NSInteger idx = [self indexOfObject:obj];
    [self replaceObjectAtIndex:idx withObject:obj1];
}

- (void)removeASameElementWithAnotherArr:(NSArray *)arr {
    for (id obj in arr) {
        if ([self containsObject:obj]) {
            [self removeOneObject:obj];
        }
    }
}

@end
